package com.yellowpage.bigdata.data;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.abel533.echarts.DataZoom;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.MarkType;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.data.LineData;
import com.github.abel533.echarts.data.PieData;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Pie;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 构造echart默认图表
 */
public class DefaultOption {
    private Option option = new Option();
    private ValueAxis valueAxis  = new ValueAxis();
    private CategoryAxis categoryAxis = new CategoryAxis();
    private DataZoom dataZoom = new DataZoom();

    public void setText(String text,String subtext){
        option.title().text(text).subtext(subtext);
    }

    public void setLegends(Object...legends){
        option.legend(legends);
    }

    public void setCategoryAxisList(List xList){
        categoryAxis.setData(xList);
    }

    public void setLineData(String legend,List yList){
        Line line = new Line();
        line.name(legend).setData(yList);
        line.markPoint().data(new LineData().type(MarkType.max).name("最大值"), new LineData().type(MarkType.min).name("最小值"));
        line.markLine().data(new LineData().type(MarkType.average).name("平均值"));
        option.series(line);
    }

    public void setBarData(String legend,List yList){
        Bar bar = new Bar();
        bar.name(legend).setData(yList);
        option.series(bar);
    }

    public void getOption(HttpServletResponse response){
        option.tooltip(Trigger.axis);
        option.toolbox().show(true).feature(Tool.mark, Tool.dataView, Tool.restore, Tool.saveAsImage);
        option.calculable(true);
        valueAxis.axisLabel().formatter("{value}");
        option.yAxis(valueAxis);
        categoryAxis.setBoundaryGap(false);
        option.xAxis(categoryAxis);
        option.setCalculable(true);
        dataZoom.setRealtime(true);
        dataZoom.setStart(0);
        dataZoom.setEnd(100);
        dataZoom.show(true);
        option.setDataZoom(dataZoom);

        //jackson 返回
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        ObjectMapper mapper = new ObjectMapper();
        //忽略空值
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        try {
            mapper.writeValue(response.getWriter(), option);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getPieOption(HttpServletResponse response) {
        option.title().x("center");
        option.tooltip(Trigger.item);
        option.legend().x("left").orient(Orient.vertical);
        option.tooltip().formatter("{a} <br/>{b} : {c} ({d}%)");
        option.toolbox().show(true).feature(Tool.dataView, Tool.restore, Tool.saveAsImage);
        option.calculable(true);

        //jackson 返回
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        ObjectMapper mapper = new ObjectMapper();
        //忽略空值
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        try {
            mapper.writeValue(response.getWriter(), option);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setPieData(String legend,List<Map> pieData){
        //饼图数据
        Pie pie = new Pie(legend);
        for(Map map : pieData) {
            //请将sql查询出来的字段定义为name,count：select a name,b count from c
            pie.data(new PieData(map.get("NAME").toString(), map.get("COUNT")));
        }
        //饼图的圆心和半径
        pie.center("50%", "60%").radius("55%");
        //设置数据
        option.series(pie);
    }

    public DefaultOption() {

    }

    public static void main(String[] args){
//        List xList = new ArrayList();
//        xList.add("一");
//        xList.add("二");
//        xList.add("三");
//
//        List yList = new ArrayList();
//        yList.add(1);
//        yList.add(2);
//        yList.add(3);
//
//        DefaultOption o = new DefaultOption();
//        o.setLegends("新增", "月");
//        o.setText("测试", "日期");
//        o.setCategoryAxisList(xList);
//        o.setLineData("新增", yList);
//        o.setLineData("月", yList);

        DefaultOption o = new DefaultOption();
        //设置所有统计纬度名称，可以设置多个 o.setLegends("新增","月新增")
        o.setLegends("一", "二", "三", "四");

        //设置图表标题和子标题
        o.setText("月新增用户分分渠道统计", "统计日期:");

        List<Map> list = new ArrayList<Map>();
        Map m1 = new HashMap();
        m1.put("name", "一");
        m1.put("count", 100);
        Map m2 = new HashMap();
        m2.put("name", "二");
        m2.put("count", 120);
        Map m3 = new HashMap();
        m3.put("name", "三");
        m3.put("count", 200);
        Map m4 = new HashMap();
        m4.put("name", "四");
        m4.put("count", 150);
        list.add(m1);
        list.add(m2);
        list.add(m3);
        list.add(m4);

        //设置显示名称和数据
        o.setPieData("新增数", list);

        System.out.println(o);
    }
}
